
package com.be.beadmin.device.impl;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.entity.DeviceInfo;
import com.be.beadmin.device.entity.DeviceInfo_;
import cn.hutool.core.util.ObjectUtil;

import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.DeviceInfoMapperEx;
import com.be.beadmin.device.repository.DeviceInfoRepository;
import com.be.beadmin.device.service.DeviceInfoService;
import com.be.beadmin.device.dto.input.DeviceInfoDto;
import com.be.beadmin.device.dto.output.DeviceInfoEx;
import com.be.beadmin.device.dto.input.DeviceInfoQueryCriteria;
import com.be.beadmin.device.mapper.DeviceInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @description 服务实现
* @author chenxu
* @date 2023-09-07
**/
@Service
@RequiredArgsConstructor
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Autowired
    private  DeviceInfoRepository deviceInfoRepository;
    @Autowired
    private  DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private  DeviceInfoMapperEx deviceInfoExMapper;

    @Override
    public Page<DeviceInfoEx> getDeviceInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<DeviceInfo> page = deviceInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(DeviceInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(DeviceInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<DeviceInfoEx> page1 = page.map(deviceInfoExMapper::toEntity);
            return page1;

    }

    @Override
    public void addDeviceInfo(DeviceInfoDto deviceInfoDto) {
            DeviceInfo result = deviceInfoRepository.findDeviceInfoByIdAndDelete(deviceInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("DeviceInfo不存在,请重新查询");
            }
            DeviceInfo deviceInfo = deviceInfoMapper.toEntity(deviceInfoDto);
            deviceInfoRepository.save(deviceInfo);
    }

    @Override
    public void updateDeviceInfo(DeviceInfoDto deviceInfoDto) {
            DeviceInfo deviceInfo = deviceInfoRepository.findDeviceInfoById(deviceInfoDto.getId());
            deviceInfo = deviceInfoMapper.toEntity(deviceInfoDto);
            deviceInfoRepository.save(deviceInfo);
    }


    @Override
    public void delDeviceInfo(String id) {
        DeviceInfo deviceInfo = deviceInfoRepository.findDeviceInfoById(id);
        deviceInfo.setDelete(1);
        deviceInfoRepository.save(deviceInfo);
    }

    @Override
    public void delDeviceInfos (List<String> ids) { }

    @Override
    public List<String> getDeviceIds() {
        List<DeviceInfo> deviceInfoList = deviceInfoRepository.findDeviceInfosByDelete(0);
        List<String> deviceIds = deviceInfoList.stream().map(DeviceInfo::getDeviceId).collect(Collectors.toList());
        return deviceIds;
    }
}